package com.foucus.myfouces.MVP.Presenler.Presenler_class;

import android.content.Context;

import com.foucus.myfouces.Api.GankService;
import com.foucus.myfouces.Base.BaseReq;
import com.foucus.myfouces.Base.Setting;
import com.foucus.myfouces.MVP.Model.BannerResp;
import com.foucus.myfouces.MVP.Model.NewestVedioListResp;
import com.foucus.myfouces.MVP.Presenler.IPresenler.INewestVideoP;
import com.foucus.myfouces.MVP.View.IView.IHomePageFG;
import com.foucus.myfouces.Resource.HttpUtil.ServiceFactory;
import com.foucus.myfouces.Resource.Util.GsonImpl;
import com.foucus.myfouces.Resource.Util.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yf-06 on 2017/7/12.
 */

public class NewestVideoP implements INewestVideoP {
    IHomePageFG iHomePageFG;
    private Context context;
    private List<NewestVedioListResp.ResultBean.DataBean> dataBeen = new ArrayList<>();
    private int page = 1;
    private static final int LOAD = 0;//加载
    private static final int REFRESH = 1;// 刷新
    private static final int MORE = 2;// 更多
    int total;

    public NewestVideoP(Context context, IHomePageFG iHomePageFG) {
        this.context = context;
        this.iHomePageFG = iHomePageFG;
    }

    @Override
    public void getNewestVideoList(final int type) {
        if (type == LOAD || type == REFRESH) {
            page = 1;
        } else {
            page = (dataBeen.size() / 10 + 1);

        }
        BaseReq req = new BaseReq();
        req.setValue("member", "null");
        req.setValue("type", "all");
        req.setValue("id", "0");
        req.setValue("lng", "100.564596");
        req.setValue("lat", "26.997575");
        req.setValue("page", page + "");
        req.setValue("page_size", "10");
        req.setValue("order", "id");
        req.setValue("sort", "desc");
        ServiceFactory
                .getInstance()
                .createService(GankService.class)
                .getNewestVideoList(req.getSecret("获取最新作品"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        iHomePageFG.onNewestVideoCompleted(dataBeen, total);
                    }

                    @Override
                    public void onError(Throwable e) {
                        iHomePageFG.onNewestVideoError();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        String response = JsonUtil.JsonToStr(responseBody);
                        boolean isflag = Setting.isRight(context, response);
                        if (isflag) {
                            NewestVedioListResp resp = GsonImpl.get().toObject(response.toString(), NewestVedioListResp.class);
                            total = resp.getResult().getMeta().getPagination().getTotal();

                            if (type == LOAD || type == REFRESH) {
                                dataBeen.clear();
                                dataBeen.addAll(resp.getResult().getData());
                            } else {
                                dataBeen.addAll(resp.getResult().getData());
                            }
//                            if (dataBeen.size() < total) {
//                                isLoading = false;
//                            }
//                            setAdapter1(total);

                        }
                    }
                });
    }
}
